package cn.buk.tms.entity.order.vas;

import cn.buk.common.util.DecimalUtil;
import cn.buk.tms.entity.base.BaseOrderInfo;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import jakarta.persistence.*;

/**
 * Created by yfdai on 2017-10-10
 * 增值服务订单
 *
 * @author yfdai
 */
@Entity
@Table(name = "vas_order", uniqueConstraints = @UniqueConstraint(
        name = "u_idx_vas_order", columnNames = {"enterprise_id", "order_no"}))
@AssociationOverrides({
        @AssociationOverride(name = "customer",
                joinColumns = @JoinColumn(name = "customer_id"),
                foreignKey = @ForeignKey(name = "fk_vas_order_customer")
        ),
        @AssociationOverride(name = "supplier",
                joinColumns = @JoinColumn(name = "supplier_id"),
                foreignKey = @ForeignKey(name = "fk_vas_order_supplier")
        ),
        @AssociationOverride(name = "paymentMethod",
                joinColumns = @JoinColumn(name = "payment_method_id"),
                foreignKey = @ForeignKey(name = "fk_vas_order_payment_method")
        )
})
public class VasOrder extends BaseOrderInfo {

  /**
   * 本地服务订单
   */
  public static final int VAS_ORDER_TYPE_LOCAL = 0;

  /**
   * VAAS平台服务订单
   */
  public static final int VAS_ORDER_TYPE_VAAS = 1;


  /**
   * 服务单号
   */
  @Column(name = "order_no", length = 10)
  private String orderNo;

  /**
   * 外部订单号
   */
  @Column(name = "out_order_no", length = 32)
  private String externalOrderNo;

  /**
   * 订单类型
   * 0/null: 本地订单
   * 1: 机场服务订单（第三方订单(vaas)
   */
  @Column(name = "order_type")
  private Integer orderType;

  /**
   * 保险订单类型
   * 0-销售订单
   * 4-退保单
   */
  @Column(name = "insurance_order_type")
  private Integer insuranceOrderType;

  /**
   * 订单创建日期，搜索时会使用
   */
  @Temporal(TemporalType.DATE)
  @Column(name = "create_date", nullable = false)
  private Date createDate;

  /**
   * 关联的机票订单
   */
  @Column(name = "flight_order_id")
  private Integer flightOrderId;

  /**
   * 商品类型： 保险，门票等
   */
  @Column(name = "product_type", updatable = false)
  private int productType;
  /**
   * 增值服务产品的代码：TieInProduct.productCode
   */
  @Column(name = "product_code")
  private String productCode;

  @Column(name = "product_name")
  private String productName;


  /**
   * 订单状态
   * 0 - 待处理
   * 1 - 处理中
   * 2 - 已处理
   * 4 - 已取消
   */
  private int status;

  /**
   * 标价
   */
  private double price;

  /**
   * 折扣金额
   */
  private double discount;

  /**
   * 成本
   */
  private double cost;

  /**
   * 数量
   */
  private int count;

  /**
   * 利润 = count * (price - discount- cost)
   */
  private double profit;

  private Double total;


  /**
   * 付款状态(只有付款类型非记账方式时才有用）
   * 0 or null 未支付
   * 1 已支付
   * 2 已销账 已支付（已销账，同步财务账单的状态）
   */
  @Column(name = "pay_status")
  private Integer payStatus;

  private String remark;


  /**
   * 联系人（代订人）
   */
  @Column(length = 20)
  private String linkman;

  @Column(length = 20, name = "link_phone")
  private String linkPhone;


  @OneToMany(mappedBy = "order", cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
  @OrderBy("name")
  private Set<VasOrderPassenger> passengers;

  @OneToMany(mappedBy = "order", cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
  @OrderBy("segOrder")
  private Set<VasOrderFlight> flights;

  /**
   * 对应的总订单id
   */
  @Column(name = "blanket_order_id")
  private Integer blanketOrderId;




  public int getCount() {
    return count;
  }

  public void setCount(int count) {
    this.count = count;
  }

  public String getRemark() {
    return remark;
  }

  public void setRemark(String remark) {
    this.remark = remark;
  }

  public String getProductCode() {
    return productCode;
  }

  public void setProductCode(String productCode) {
    this.productCode = productCode;
  }

  public String getProductName() {
    return productName;
  }

  public void setProductName(String productName) {
    this.productName = productName;
  }


  public double getProfit() {
    return profit;
  }

  public void setProfit(double profit) {
    this.profit = profit;
  }

  public int getStatus() {
    return status;
  }

  public void setStatus(int status) {
    this.status = status;
  }

  public Date getCreateDate() {
    return createDate;
  }

  public void setCreateDate(Date createDate) {
    this.createDate = createDate;
  }


  public Integer getPayStatus() {
    return payStatus == null ? 0 : payStatus;
  }

  public void setPayStatus(Integer payStatus) {
    this.payStatus = payStatus;
  }

  public double getPrice() {
    return price;
  }

  public void setPrice(double price) {
    this.price = price;
  }

  public double getDiscount() {
    return discount;
  }

  public void setDiscount(double discount) {
    this.discount = discount;
  }

  public double getCost() {
    return cost;
  }

  public void setCost(double cost) {
    this.cost = cost;
  }

  public Set<VasOrderPassenger> getPassengers() {
    if (passengers == null) {
      passengers = new HashSet<>();
    }
    return passengers;
  }

  public void setPassengers(Set<VasOrderPassenger> passengers) {
    this.passengers = passengers;
  }

  public double getTotal() {
    return total == null ? 0 : total;
  }

  public void setTotal(Double total) {
    this.total = total;
  }

  public int getFlightOrderId() {
    return flightOrderId == null ? 0 : flightOrderId;
  }

  public void setFlightOrderId(Integer flightOrderId) {
    this.flightOrderId = flightOrderId;
  }

  public int getProductType() {
    return productType;
  }

  public void setProductType(int productType) {
    this.productType = productType;
  }


  public String getOrderNo() {
    return orderNo;
  }

  public void setOrderNo(String orderNo) {
    this.orderNo = orderNo;
  }


  public String getLinkman() {
    return linkman;
  }

  public void setLinkman(String linkman) {
    this.linkman = linkman;
  }

  public String getLinkPhone() {
    return linkPhone;
  }

  public void setLinkPhone(String linkPhone) {
    this.linkPhone = linkPhone;
  }


  public Set<VasOrderFlight> getFlights() {
    if (flights == null) {
      flights = new HashSet<>();
    }
    return flights;
  }

  public void setFlights(Set<VasOrderFlight> flights) {
    this.flights = flights;
  }

  public int getOrderType() {
    return orderType == null ? 0 : orderType;
  }

  public void setOrderType(int orderType) {
    this.orderType = orderType;
  }

  public String getExternalOrderNo() {
    return externalOrderNo;
  }

  public void setExternalOrderNo(String externalOrderNo) {
    this.externalOrderNo = externalOrderNo;
  }

  public Integer getBlanketOrderId() {
    return blanketOrderId;
  }

  public void setBlanketOrderId(Integer blanketOrderId) {
    this.blanketOrderId = blanketOrderId;
  }

  public int getInsuranceOrderType() {
    return insuranceOrderType == null ? 0 : insuranceOrderType;
  }

  public void setInsuranceOrderType(Integer insuranceOrderType) {
    this.insuranceOrderType = insuranceOrderType;
  }

  public void calc() {
    this.total = this.count * (this.price - this.discount);
    this.profit = this.total - this.count * this.cost;

    this.total = DecimalUtil.keepScale2(this.total);
    this.profit = DecimalUtil.keepScale2(this.profit);
  }
}
